// server.js
require('dotenv').config();
const express = require('express');
const validateParams = require('../Middleware/validation');
const weatherRoutes = require('../routes/weather');

const app = express();
const port = process.env.PORT || 3000;

// 中间件
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// 核心路由
app.use('/api/weather', validateParams, weatherRoutes);

// 错误处理中间件
app.use((err, req, res, next) => {
  console.error('全局错误处理:', err);
  res.status(500).json({ error: '服务器内部错误' });
});

app.listen(port, () => {
  console.log(`服务运行中: http://localhost:${port}`);
});